﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace _05.regex
{
    //正则表达式
    class Program
    {
        static void Main(string[] args)
        {
            //StringBuilderTest();
#if true
            {
                string s = "I am a BLUE  cat! ";
                string r = Regex.Replace(s, "^", "<开始>"); //f**k, 原来是具体位置！
                Console.WriteLine(r);

                r = Regex.Replace(s, "$", "<结束>");
                Console.WriteLine(r);

                r = Regex.Replace(s, @"\b", "|"); //单词边界
                Console.WriteLine(r);

                r = Regex.Replace(s, @"\w", "<单词>");
                Console.WriteLine(r);

                r = Regex.Replace(s, @"\s", "-");//空白字符，都替换成*
                Console.WriteLine(r);

                r = Regex.Replace(s, @"[^abc]", "*");//除了abc的字符，都替换成*
                Console.WriteLine(r);
            }
#endif
#if true
            { //看看如何修改字符串 - $&符号代表原字符串！   $$代表一个$
                string s = "the price is: 15.34"; //修改成 "the price is: $15.34"
                string rep = @"$$$&"; //卧槽
                //string rep = @"￥$&"; //卧槽
                //var r = Regex.Replace(s, @"^\d+.\d+\$", rep);
                var r = Regex.Replace(s, @"\b\d+.\d+\b", rep);
                Console.WriteLine(r);
            }
#endif
#if false
            { //匹配重复的内容
                string s = "this is cat cat";
                string p = @"\b(\w+)\b\W+(\1)"; //() (\1) 第一组重复
                var m = Regex.Match(s, p); //默认只匹配第一个
                while (m.Success)
                {
                    Console.WriteLine("dup found: {0}, @{1}", m.Value, m.Index);
                    m = m.NextMatch();
                }
            }
#endif
#if false
            {
                string s = "0123456789";
                bool m = Regex.IsMatch(s, @"^\d*$");//完全数字
                Console.WriteLine(m);

                s = "abc0123d";
                m = Regex.IsMatch(s, @"\d+"); //KEY: 包含正则部分，就满足！
                Console.WriteLine(m);
            }
#endif
#if false
            { //match, group
                string names = "zhangsan;lisi,wangwu.liuliu,gouqi";
                var arr = Regex.Split(names, @"[;,.]");
                foreach (var i in arr)
                {
                    Console.WriteLine(i);
                }

                //Matches获取匹配的内容，多个！Match是单个。
                var mm = Regex.Matches(names, @"\b\w+\b");
                Console.WriteLine(mm.Count);
                Console.WriteLine(mm[0]);
                Console.WriteLine(mm[1]);
                foreach (var m in mm)
                {
                    Console.WriteLine(m);
                }

            }
#endif

            Console.ReadKey();
        }

        static void StringBuilderTest()
        {
            var sb = new StringBuilder("init str");
            sb.Append(1);
            sb.Append(true);
            sb.Append(1.23d);
            sb.AppendFormat("{0} : {1}", true, false);
            Console.WriteLine(sb.ToString());

            sb.Insert(0, "hehehe");
            Console.WriteLine(sb.ToString());

            sb.Remove(0, 6);
            Console.WriteLine(sb.ToString());

            sb.Replace("init", "aaaa");
            Console.WriteLine(sb.ToString());
        }
    }
}
